package dataPersistence;
//took this code from book club at the advice of DR. Rodham;

import java.sql.*;
import java.util.ArrayList;
import java.io.*;

import com.itextpdf.text.ImgJBIG2;

import common.Result;



public class TransactionManager {

	private static Connection _connection;

	public static Result initialize() {
		Result result = new Result();
		try 
		{
			final String driver = "org.apache.derby.jdbc.EmbeddedDriver";
			Class.forName(driver);
			result.setStatus(true);

		}
		catch(ClassNotFoundException e) 
		{
			result .setMessage("Could not load Cerby database driver");
			return result;
		}
		return result;
	}

	//
	//
	//

	private TransactionManager() {
		return;
	}

	public static Connection getConnection() {
		return _connection;
	}

	public static Result begin() {

		Result result = new Result();
		//lib/derby-10.5/bin
		String dbName = "lib"+File.separator+"derby-10.5"+File.separator+"bin"+File.separator+"db" + File.separator + "hit";
		//String dbName = "db" + File.separator + "hit";
		String connectionURL = "jdbc:derby:" + dbName + ";create=false";

		try
		{
			assert (_connection == null);

			_connection = DriverManager.getConnection(connectionURL);
			_connection.setAutoCommit(false);
			result.setStatus(true);

		}
		catch (SQLException e) 
		{
			result.setMessage(e.getMessage());
		}
		return result;


	}

	public static Result end(boolean commit) {

		assert (_connection != null);		
		Result result = new Result();

		try 
		{
			if (commit) 
			{
				_connection.commit();
			}
			else 
			{
				_connection.rollback();
			}
			result.setStatus(true);

		}
		catch (SQLException e) 
		{
			result.setMessage(e.getMessage());
		}
		finally 
		{
			safeClose(_connection);
		}

		_connection = null;
		return result;
	}

	public static void safeClose(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
			}
			catch (SQLException e) {
				// ...
			}
		}
	}

	public static void safeClose(PreparedStatement stmt) {
		if (stmt != null) {
			try {
				stmt.close();
			}
			catch (SQLException e) {
				// ...
			}
		}
	}

	public static void safeClose(ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			}
			catch (SQLException e) {
				// ...
			}
		}
	}

	public static void createDB()
	{
		ArrayList<String> commands= new ArrayList<String>();
		boolean commit=true;

		String st= "";
		String line="";
		File f = new File("createDB.sql");
		BufferedReader in;
		try 
		{
			in = new BufferedReader( new FileReader(f));

			while ((line=in.readLine())!=null)
			{

				line=cleanLine(line);
				if(line.contains(";"))
				{
					line=line.replace(";", "");
					st+=line;
					commands.add(st);
					st="";
				}
				else
					st+=line;	
			}
		} catch (FileNotFoundException e) {
			commit=false;
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			commit=false;
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Result r = new Result();
		r=	begin();
		System.err.println(r.getMessage());

		for(String str : commands)
		{
			System.out.println(str);
			Statement stat;
			try 
			{
				stat = TransactionManager.getConnection().createStatement();
				stat.execute(str);
			} 
			catch (SQLException e) 
			{
				e.printStackTrace();
				if(!str.contains("DROP"))
				{
					commit=false;
					break;
				}
				else
				{

					continue;
				}

			}

		}
		end(commit);




	}

	private static String cleanLine(String line) {
		if(line.contains("--"))
		{
			return line.substring(0, line.indexOf("--"));
		}
		else 
			return line;

	}

}
